#include <iostream>
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <unistd.h>

typedef struct {
    char name[20];
    int time;
    int start;
    int end;
}RaceArg;




void* func(void*arg){
    RaceArg*r=(RaceArg*)arg;
    int i=r->start;

    for(;i<=r->end;i++){
        std::cout<<"name:"<<r->name<<" thread id"<<pthread_self()<<" num:"<<i<<std::endl;

    }

    // //int distance=(int)arg;
    // int i;
    // for(i=1;i<=50;i++){
    //     std::cout<<"id:"<<pthread_self()<<" num:"<<i<<std::endl;
    //     printf("\n");
    //     int time=(int)(drand48()*10000);
    //     usleep(time);
    // }


    return (void*)9;
}

int main(){

    int err;

    RaceArg  r_a={"rabbit",(int)(drand48()*10000),20,50};
     RaceArg  t_a={"tuetle",(int)(drand48()*10000),10,60};

    pthread_t rabbit,turtle;

    if((err=pthread_create(&rabbit,NULL,func,(void*)&r_a)!=0)){

        perror("pthread_create");

    }

    if((err=pthread_create(&turtle,NULL,func,(void*)&t_a))!=0){

        perror("pthread_create");

    }


    //主控线程调用pthread_join()自己会阻塞//知道线程执行结束才会执行主控线程本身
    
    pthread_join(rabbit,NULL);
    pthread_join(rabbit,NULL );

 
    std::cout<<"main thread"<<pthread_self()<<std::endl;
    




    return 0;
}